package com.wj.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import com.wj.pojo.CustomWorks;
import com.wj.service.ICustomWorksService;
import com.wj.utils.FastDFSUtil;
import com.wj.utils.RespUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wj
 * @since 2022-04-14
 */
@RestController
@RequestMapping("/custom/works")
public class CustomWorksController {
    @Resource
    private ICustomWorksService customWorksService;

    @DeleteMapping("/")
    public RespUtil deleteWorks(@RequestBody Integer[] ids) {
        return customWorksService.deleteWorks(ids);
    }

    @GetMapping("/searchWorks")
    public List<CustomWorks> searchWorks(String searchText){
        System.out.println(searchText);
        return customWorksService .searchWorks(searchText);
    }

    @PostMapping("/")
    public RespUtil addCustomWorks(@RequestParam(value = "img",required = false) MultipartFile[] img,@RequestParam("customWorksJson") String customWorksJson) {
        CustomWorks customWorks = JSON.parseObject(customWorksJson, CustomWorks.class);
        if (customWorks == null && customWorks.getTbAccount() == null) {
            return RespUtil.error("请输入淘宝账号!");
        }
        if (!ArrayUtils.isEmpty(img)) {
            for (MultipartFile multipartFile : img) {
                String[] upload = FastDFSUtil.upload(multipartFile);
                String trackerUrl = FastDFSUtil.getTrackerUrl();
                trackerUrl += upload[0] + "/";
                trackerUrl += upload[1];
                //设置图片的路径
                customWorks.setImagePath(trackerUrl);
                customWorksService.insertBackID(customWorks);
                customWorks = customWorksService.getById(customWorks.getId());
            }
            return RespUtil.success("已提交",customWorks);
        }
        customWorksService.insertBackID(customWorks);
        customWorks = customWorksService.getById(customWorks.getId());
        return RespUtil.success("已提交",customWorks);
    }

    @GetMapping("/")
    public List<CustomWorks> getAllCustomWorks() {
        return customWorksService.list();
    }

    @PostMapping("/update")
    public RespUtil updateCustomWorks(@RequestParam(value = "img", required = false) MultipartFile[] img, @RequestParam("customWorksJson") String customWorksJson) {
        CustomWorks customWorks = JSON.parseObject(customWorksJson, CustomWorks.class);
        if (customWorks == null && customWorks.getTbAccount() == null) {
            return RespUtil.error("请输入淘宝账号!");
        }
        //更新操作
        if (!ArrayUtils.isEmpty(img)) {
            //先把所有关于这个id的作品图片删除
            if (customWorks.getImagePath()!=null){
                String[] split = customWorks.getImagePath().split("8888/");
                String[] split1 = split[1].split("/", 2);
                try {
                    FastDFSUtil.deleteFile(split1[0], split1[1]);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            for (MultipartFile multipartFile : img) {
                String[] upload = FastDFSUtil.upload(multipartFile);
                String trackerUrl = FastDFSUtil.getTrackerUrl();
                trackerUrl += upload[0] + "/";
                trackerUrl += upload[1];
                //设置图片的路径
                customWorks.setImagePath(trackerUrl);
                customWorks.setCreateTime(LocalDateTime.now());
                customWorksService.updateById(customWorks);
                customWorks = customWorksService.getById(customWorks.getId());
            }
            return RespUtil.success("更新成功",customWorks);
        }
        customWorks.setCreateTime(LocalDateTime.now());
        customWorksService.updateById(customWorks);
        customWorks = customWorksService.getById(customWorks.getId());
        return RespUtil.success("更新成功",customWorks);
    }
}
